//
//  SJHomePageVC.m
//  SJPay
//
//  Created by 凯朱 on 2024/11/26.
//

#import "SJHomeVC.h"
#import "SJHomePageView.h"
#import "SJHomeListCell.h"
#import "SJAuthVC.h"
static  NSString *const kCellId = @"SJHomeListCell";

@interface SJHomeVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>{
    NSInteger _curIndex;
}
@property (nonatomic, strong)SJHomePageView *homeView;
@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)NSMutableArray *dataArray;
@property (nonatomic, strong)UICollectionViewFlowLayout *layout;
@end

@implementation SJHomeVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _curIndex = 1;
    _homeView = [SJHomePageView viewFromXIB];
    _homeView.frame = CGRectMake(0, 0, kScreenHeight, MAX(800,self.tableView.height));
    [self.view addSubview:self.tableView];
    self.tableView.tableHeaderView = _homeView;
    kWeakSelf(self);
    [_homeView setBtnBlock:^(NSInteger btnIndex) {
        [weakself selectIndex:btnIndex];
    }];
    [_homeView.collectBgView addSubview:self.collectionView];
}

-(void)selectIndex:(NSInteger)index{
    _curIndex = index;
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_curIndex-1 inSection:0];
    [_collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
    [_collectionView reloadData];
}

#pragma mark - ================<懒加载>===============
-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,kScreenWidth,kScreenHeight-SafeAreaBottomHeight-50) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = KClearColor;
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 3;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SJHomeListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellId forIndexPath:indexPath];
    
    if (indexPath.row == 0) {
        
        cell.titleLbl.text = kLocalString(@"认证资料");
        cell.icon.image = [UIImage imageNamed:@"icon_home_ziliao"];
    }else if (indexPath.row == 1){
        cell.titleLbl.text = kLocalString(@"通过审核");
        cell.icon.image = [UIImage imageNamed:@"icon_home_auth"];
    }else if (indexPath.row == 2){
        cell.titleLbl.text = kLocalString(@"立即放款");
        cell.icon.image = [UIImage imageNamed:@"icon_home_jie"];
    }
    if (_curIndex == indexPath.row+1) {
        cell.contentView.backgroundColor = kHexColor(@"#5E81FF");
        cell.titleLbl.textColor = KWhiteColor;
    }else{
        cell.contentView.backgroundColor = kHexColor(@"#647998");
        cell.titleLbl.textColor = kHexColor(@"#A6CEF9");
        
    }
        return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
}


#pragma mark - ================<懒加载>===============
-(UICollectionView *)collectionView{
    if (_collectionView == nil) {
        _layout = [[UICollectionViewFlowLayout alloc] init];
        _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _layout.itemSize = CGSizeMake(130, 90);
        _layout.minimumLineSpacing = 20;
        _layout.minimumInteritemSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth-30, 90) collectionViewLayout:_layout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerNib:[UINib nibWithNibName:kCellId bundle:nil] forCellWithReuseIdentifier:kCellId];
    }
    return _collectionView;
}

@end
